import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode{
        public TreeNode right;
        public TreeNode left;
        public char val;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createBinaryTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.right=H;

        return A;
    }

    TreeNode root;
    public int nodeSize;
    // 前序遍历
    public void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);

    };
    // 后序遍历
    void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }


    // 获取树中节点的个数
//        public int size(TreeNode root){
//            if(root==null){
//                return 0;
//            }
//            nodeSize++;
//            size(root.left);
//            size(root.right);
//            return nodeSize;
//        }
    public int size(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftSize=size(root.left);
        int rightSize=size(root.right);
        return leftSize+rightSize+1;
        //或者
//        return size(root.left)+size(root.right)+1;
    }
        // 获取叶⼦节点的个数
    public int getLeafNodeCount(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null) {
            return 1;
        }
        return getLeafNodeCount(root.left)+getLeafNodeCount(root.right);
    }
        // ⼦问题思路-求叶⼦结点个数

        // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root==null){
            return 0;
        }
        if(k==1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }
        // 获取⼆叉树的⾼度
    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftHeight=getHeight(root.left);
        int rightHeight=getHeight(root.right);
        return Math.max(leftHeight,rightHeight)+1;
    }
        // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        TreeNode leftVal=find(root.left,val);
        if(leftVal!=null){
            return leftVal;
        }
        return find(root.right,val);
    }

        //层序遍历
    public void levelOrder(TreeNode root){
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }
        // 判断⼀棵树是不是完全⼆叉树
    public boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur!=null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                //遇到null就break出循环
                break;
            }
        }
        //遍历看队列是否有不是null的
        while(!queue.isEmpty()){
            TreeNode cur=queue.peek();
            //如果有不是null的，就返回false
            //不让就出队列
            if(cur==null){
                queue.poll();
            }else{
                return false;
            }
        }
        return true;
    }
}
